package com.car.advertising.view.viewAdapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.car.advertising.R;
import com.car.advertising.entity.MeEntity;
import com.support.widget.interfacelistener.OnRecyclerViewItemClickListener;
import com.support.widget.interfacelistener.OnRecyclerViewItemLongClickListener;
import com.support.widget.layout.adapter.BaseRecyclerViewAdapter;
import com.support.widget.layout.holder.BaseViewHolder;

import java.util.List;

/**
 * 开发者：LuoYi
 * Time: 2017 16:52 2017/4/13 04
 */

public class NavigationMenuAdapter extends BaseRecyclerViewAdapter<MeEntity, BaseViewHolder> {

    public NavigationMenuAdapter(Context context, List<MeEntity> strings) {
        super(context, strings);
    }

    public void setRefreshList(List<MeEntity> strings) {
        this.mDatas = strings;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    protected void bindView(BaseViewHolder holder, int position, MeEntity model) {
        MenuHolder menuHolder = (MenuHolder) holder;
        if (position == 0 || position == 1) {
            menuHolder.menuView.setVisibility(View.VISIBLE);
        } else {
            menuHolder.menuView.setVisibility(View.GONE);
        }
        if (position == 6) {
            menuHolder.contentRl.setVisibility(View.GONE);
            menuHolder.logoutTv.setVisibility(View.VISIBLE);
            menuHolder.logoutTv.setText(model.getNavigtion());
        } else {
            menuHolder.contentRl.setVisibility(View.VISIBLE);
            menuHolder.logoutTv.setVisibility(View.GONE);
            setTextLeftImg(menuHolder.headTv, model.getPositionItem());
            menuHolder.headTv.setText(model.getNavigtion());
            menuHolder.rightTv.setText(model.getNewContent());
            menuHolder.rightIv.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.navaigation_menu_list_view_item, parent, false);
        return new MenuHolder(mContext, inflate, this, getClickListener(), getLongClickListener());
    }

    class MenuHolder extends BaseViewHolder implements OnRecyclerViewItemClickListener, OnRecyclerViewItemLongClickListener {

        public OnRecyclerViewItemClickListener mListener;
        public OnRecyclerViewItemLongClickListener mLongClickListener;
        public View menuView;
        public TextView headTv;
        public TextView rightTv;
        public ImageView rightIv;
        public RelativeLayout contentRl;
        public TextView logoutTv;

        public MenuHolder(Context context, View rootView, RecyclerView.Adapter adapter, OnRecyclerViewItemClickListener clickListener, OnRecyclerViewItemLongClickListener longClickListener) {
            super(context, rootView, adapter, clickListener, longClickListener);
            menuView = (View) rootView.findViewById(R.id.menu_view);
            contentRl = (RelativeLayout) rootView.findViewById(R.id.menu_content_rl);
            logoutTv = (TextView) rootView.findViewById(R.id.menu_logout_tv);
            headTv = (TextView) rootView.findViewById(R.id.menu_head_tv);
            rightTv = (TextView) rootView.findViewById(R.id.menu_right_tv);
            rightIv = (ImageView) rootView.findViewById(R.id.menu_right_iv);
            this.mListener = clickListener;
            this.mLongClickListener = longClickListener;
            rootView.setOnClickListener(this);
            rootView.setOnLongClickListener(this);
        }

        @Override
        public void onItemClick(ViewGroup parent, View view, int position) {
            if (mListener != null) {
                mListener.onItemClick(parent, view, position);
            }
        }

        @Override
        public void onItemLongClick(ViewGroup parent, View view, int position) {
            if (mLongClickListener != null) {
                mLongClickListener.onItemLongClick(parent, view, position);
            }
        }
    }

    private void setTextLeftImg(TextView textView, int iconId) {
        int imgId = R.mipmap.menu_password_icon;
        switch (iconId) {
            case 0:
                imgId = R.mipmap.menu_password_icon;
                break;
            case 1:
                imgId = R.mipmap.menu_about_icon;
                break;
            case 2:
                imgId = R.mipmap.menu_feedback_icon;
                break;
            case 3:
                imgId = R.mipmap.menu_help_icon;
                break;
            case 4:
                imgId = R.mipmap.menu_the_prize_icon;
                break;
            case 5:
                imgId = R.mipmap.menu_authentication_icon;
                break;
            default:
                break;
        }
        Drawable drawable = mContext.getResources().getDrawable(imgId);
        // 这一步必须要做,否则不会显示.
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        textView.setCompoundDrawables(drawable, null, null, null);
    }
}
